/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fxbank.netbeans.plugins.openfile;

import com.fxbank.netbeans.errors.RootException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.Action;
import javax.swing.JOptionPane;
import javax.swing.text.JTextComponent;
import javax.swing.text.StyledDocument;
import org.netbeans.api.editor.EditorRegistry;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;
import org.netbeans.modules.editor.NbEditorUtilities;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.cookies.EditorCookie;

import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.nodes.Node;
import org.openide.util.ContextAwareAction;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle.Messages;
import org.openide.util.actions.CallableSystemAction;

/**
 *
 * @author alexey
 */
@ActionID(category = "Edit",
id = "com.fxbank.netbeans.plugins.openfile.FactoryFinderAction")
@ActionRegistration(displayName = "#CTL_FactoryFinder")
@ActionReferences({
    @ActionReference(path = "Editors/Popup", position = 0)
})
@Messages("CTL_FactoryFinder=Open factory.xml")
public class FactoryFinderAction implements  ActionListener {

    public FactoryFinderAction() {
    }

//    private final EditorCookie context;
//
//    public FactoryFinderAction(EditorCookie context) {
//        this.context = context;
//    }

    
    
    @Override
    public void actionPerformed(ActionEvent ev) {
        
//        System.out.println(""+org.netbeans.lib.editor.util.swing.DocumentUtilities.getText(context.getDocument()));
 
        JTextComponent target = EditorRegistry.lastFocusedComponent();
        StyledDocument styledDoc = (StyledDocument) target.getDocument();
//        TokenHierarchy hi = TokenHierarchy.get(styledDoc);
//        TokenSequence<HTMLTokenId> ts = hi.tokenSequence(HTMLTokenId.language());
        
        //Start a new thread for opening the HTML document:
//        OpenHTMLThread run = new OpenHTMLThread(styledDocdoc, identifier);
//        RequestProcessor.getDefault().post(run);
        
        FileObject fo = NbEditorUtilities.getFileObject(styledDoc);
//        System.out.println(""+fo.getPath());
//        try {
//            FileObject projectFO = ProjectManager.getDefault().findProject(fo).getProjectDirectory();
//            System.out.println("proj: "+projectFO.getPath());
//        } catch (IOException ex) {
//            Exceptions.printStackTrace(ex);
//        } catch (IllegalArgumentException ex) {
//            Exceptions.printStackTrace(ex);
//        }
        
            
        

        
        Project project = FileOwnerQuery.getOwner(fo);
        System.out.println(""+project.getProjectDirectory().getPath());
        
        File factoryXML = new File(project.getProjectDirectory().getPath().concat(File.separator).
                concat("web").concat(File.separator).concat("WEB-INF").concat(File.separator).
                concat("conf").concat(File.separator).concat("factory.xml"));
        
        try{
            
            if (!factoryXML.isFile()) {
                throw new RootException();
            }
            
            FileObject factoryXMLFO = org.openide.filesystems.FileUtil.toFileObject(factoryXML);
            System.out.println("file object is valid = "+factoryXMLFO.isValid());
            System.out.println("file is = "+factoryXML.isFile());
        
            DataObject newDo = DataObject.find(factoryXMLFO); 
            final Node node = newDo.getNodeDelegate(); 
            Action a = node.getPreferredAction(); 
            if (a instanceof ContextAwareAction) { 
                   a = ((ContextAwareAction) a).createContextAwareInstance(node.getLookup()); 
            } 
            if (a != null) { 
                  a.actionPerformed(new ActionEvent(node, ActionEvent.ACTION_PERFORMED, "")); // NOI18N 
            }
        } catch (Throwable ex) {
            JOptionPane.showMessageDialog(null, String.format("Can't find 'factory.xml'"), "Not found", JOptionPane.WARNING_MESSAGE);
            ex.printStackTrace();
        }
        
//        try {
//            new ROEditor(factoryXMLFO).open();
//        } catch (DataObjectNotFoundException ex) {
//            Exceptions.printStackTrace(ex);
//        }
        
        
        
    }

//    @Override
//    public void performAction() {
//        //XXX
//    }
//
//    @Override
//    public String getName() {
//        return "Open factory.xml";
//    }
//
//    @Override
//    public HelpCtx getHelpCtx() {
//        //XXX
//        return HelpCtx.DEFAULT_HELP;
//    }
    
}